from django.db import models
from dvadmin.utils.models import CoreModel

# Create your models here.

class Database(CoreModel):
    """
    数据库信息表（含超级管理账密）
    """
    host = models.CharField(max_length=64, verbose_name='主机名')
    host_ip = models.CharField(max_length=128, verbose_name='主机IP')
    name = models.CharField(max_length=64, verbose_name='数据库名称')
    env = models.CharField(max_length=8, null=True, blank=True, verbose_name='环境')
    product = models.CharField(max_length=32, null=True, blank=True, verbose_name='发行商')
    edition = models.CharField(max_length=64, null=True, blank=True, verbose_name='版')
    version = models.CharField(max_length=32, null=True, blank=True, verbose_name='版本')
    release = models.CharField(max_length=64, null=True, blank=True, verbose_name='发布')
    sid = models.CharField(max_length=32, verbose_name='Service ID')
    ipaddress = models.CharField(max_length=128, verbose_name='服务器IP')
    port = models.CharField(max_length=64, verbose_name='服务器端口')
    log_model = models.CharField(max_length=32, null=True, blank=True, verbose_name='日志模式')
    role = models.CharField(max_length=32, null=True, blank=True, verbose_name='角色')
    characterset = models.CharField(max_length=32, null=True, blank=True, verbose_name='字符集')
    encryption = models.CharField(max_length=32, null=True, blank=True, verbose_name='加密方式')
    superuser = models.CharField(max_length=32, verbose_name='DBA账户')
    superpwd = models.CharField(max_length=64, verbose_name='DBA密码')
    status = models.CharField(max_length=32, null=True, blank=True, verbose_name='状态')
    online_form = models.CharField(max_length=32, null=True, blank=True, verbose_name='上线表单')
    offline_form = models.CharField(max_length=32, null=True, blank=True, verbose_name='下线表单')

    class Meta:
        db_table = 'secmdb_database'
        verbose_name = "数据库信息表"
        verbose_name_plural = verbose_name
        ordering = ('product', 'env', 'name')

class DatabaseUser(CoreModel):
    """
    数据库用户表
    """
    database = models.ForeignKey(Database, on_delete=models.SET_DEFAULT, default=None, verbose_name='所属数据库')
    user = models.CharField(max_length=32, verbose_name='用户名')
    current_pwd = models.CharField(max_length=64, null=True, blank=True, verbose_name='当前密码')
    in_use = models.BooleanField(default=True, verbose_name="是否在用")
    last_change = models.DateTimeField(null=True, blank=True, verbose_name='上次修改时间')
    last_pwd = models.CharField(max_length=64, null=True, blank=True, verbose_name='上次密码')
    is_change = models.BooleanField(default=False, verbose_name="是否需要修改")
    next_pwd = models.CharField(max_length=64, null=True, blank=True, verbose_name='下次密码')
    next_change = models.DateTimeField(null=True, blank=True, verbose_name='下次修改时间')
    create_form = models.CharField(max_length=64, null=True, blank=True, verbose_name='创建单号')
    del_form = models.CharField(max_length=64, null=True, blank=True, verbose_name='删除单号')

    class Meta:
        db_table = 'secmdb_database_user'
        verbose_name = "数据库用户表"
        verbose_name_plural = verbose_name
        ordering = ('database', 'user' , 'is_change' )


class Tnsname(CoreModel):
    """
    tnsnames.ora
    """
    name = models.CharField(max_length=128, verbose_name='TNS名称')
    target_db = models.CharField(max_length=64, verbose_name='目标数据库')
    target_ip = models.CharField(max_length=128, verbose_name='目标IP')
    target_port = models.CharField(max_length=64, verbose_name='目标端口')
    host = models.CharField(max_length=64, verbose_name='主机名')
    host_ip = models.CharField(max_length=128, verbose_name='主机IP')
    status = models.CharField(max_length=32, null=True, blank=True, verbose_name='状态')
    tns_full = models.TextField(null=True, blank=True, verbose_name='TNS全文')

    class Meta:
        db_table = 'secmdb_tnsname'
        verbose_name = "TNS信息表"
        verbose_name_plural = verbose_name
        ordering = ('host', 'target_db')

class DatabaseLink(CoreModel):
    """
    链接信息表
    """
    name = models.CharField(max_length=64, verbose_name='链接名称')
    owner = models.CharField(max_length=32, verbose_name='拥有者')
    link_to = models.CharField(max_length=64, verbose_name='目标数据库')
    link_to_ip = models.CharField(max_length=128, verbose_name='目标IP')
    link_to_port = models.CharField(max_length=64, verbose_name='目标端口')
    link_type = models.CharField(max_length=32, null=True, blank=True, verbose_name='类型')
    user_mapping = models.CharField(max_length=32, null=True, blank=True, verbose_name='用户映射')
    database = models.ForeignKey(Database, on_delete=models.SET_DEFAULT, default=None, verbose_name='所属数据库')
    status = models.CharField(max_length=32, null=True, blank=True, verbose_name='状态')
    create_form = models.CharField(max_length=64, null=True, blank=True, verbose_name='创建单号')
    remove_form = models.CharField(max_length=64, null=True, blank=True, verbose_name='删除单号')

    class Meta:
        db_table = 'secmdb_database_link'
        verbose_name = "链接信息表"
        verbose_name_plural = verbose_name
    
    









